﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using System.Diagnostics;

namespace pklib {
	public class DataObject {

		public virtual void CopyValuesFrom(Object Source) {
			Type t = this.GetType(), st = Source.GetType();
			if (st.Name != t.Name)
				throw new Exception(String.Format("Unable to ShallowCopy(), Source Type {0} does not match this Type {1}", Source.GetType().Name, t.Name));

			foreach (PropertyInfo pi in t.GetProperties()) {
				Type pt = pi.GetValue(this, null).GetType();
				if(pt.GetMethod("CopyValuesFrom") != null) {
					pt.GetMethod("CopyValuesFrom").Invoke(pi.GetValue(this, null), (new object[] { pi.GetValue(Source, null) }) );
				} else {
					pi.SetValue(this, pi.GetValue(Source, null), null);
				}
			}
			foreach (FieldInfo fi in t.GetFields()) {
				Type ft = fi.GetValue(this).GetType();
				if (ft.GetMethod("CopyValuesFrom") != null) {
					ft.GetMethod("CopyValuesFrom").Invoke(fi.GetValue(this), (new object[] { fi.GetValue(Source) }));
				} else {
					fi.SetValue(this, fi.GetValue(Source));
				}
			}

			return;
		}
	}
}
